package com.oocl.customersystem.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;

import com.oocl.customersystem.dao.CustomerDao;
import com.oocl.customersystem.pojo.Customer;
import com.oocl.customersystem.util.DBUtil;
import com.oocl.customersystem.util.DBUtilTest;

public class CustomerDaoImpl implements CustomerDao {

	@Override
	public int addCustomer(Customer c) throws SQLException {
		String sql="insert into customer (cid,cname,pwd) values(?,?,?)";
		int m=0;
		Connection con=DBUtil.createConnection();
		PreparedStatement pst=null;
		pst=con.prepareStatement(sql);
		String cid=UUID.randomUUID().toString();
		pst.setString(1, cid);
		pst.setString(2, c.getCname());
		pst.setString(3, c.getPwd());
		m=pst.executeUpdate();
		DBUtil.close(con, pst, null);
		return m;
	}

	@Override
	public Customer loadCustomer(String cname) throws SQLException {
		String sql="select * from customer where cname=?";
		Connection con=DBUtil.createConnection();
		PreparedStatement pst=null;
		ResultSet rs=null;
		Customer customer=null;
		pst=con.prepareStatement(sql);
		pst.setString(1, cname);
		rs=pst.executeQuery();
		if (rs.next()) {
			customer=new Customer(rs.getString("cid"), rs.getString("cname"), rs.getString("pwd"));
		}
		DBUtil.close(con, pst, rs);	
		return customer;
	}

}
